package 尊享面试.数组_字符串;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class a624数组列表中的最大距离 {
    public static void main(String[] args) {
        Solution solution = new Solution();
        List<List<Integer>> arrays = new ArrayList<>();
        arrays.add(Arrays.asList(1, 5));
        arrays.add(Arrays.asList(3, 4));
        System.out.println(solution.maxDistance(arrays));
    }

    static class Solution {
        public int maxDistance0(List<List<Integer>> arrays) {
            int ans = 0;
            int size = arrays.size();
            for (int i = 0; i < size - 1; i++) {
                for (int j = i + 1; j < size; j++) {
                    List<Integer> arrayi = arrays.get(i);
                    List<Integer> arrayj = arrays.get(j);
                    ans = Math.max(ans, Math.abs(arrayj.get(0) - arrayi.get(arrayi.size() - 1)));
                    ans = Math.max(ans, Math.abs(arrayi.get(0) - arrayj.get(arrayj.size() - 1)));
                }
            }
            return ans;
        }

        public int maxDistance(List<List<Integer>> arrays) {
            int ans = 0;
            int size = arrays.get(0).size();
            int min = arrays.get(0).get(0);
            int max = arrays.get(0).get(size - 1);
            for (int i = 1; i < arrays.size(); i++) {
                size = arrays.get(i).size();
                ans = Math.max(ans, Math.abs(min - arrays.get(i).get(size - 1)));
                ans = Math.max(ans, Math.abs(max - arrays.get(i).get(0)));
                min = Math.min(min, arrays.get(i).get(0));
                max = Math.max(max, arrays.get(i).get(size - 1));
            }
            return ans;
        }
    }
}

